package com.mdxq.mall.order.controller;

import com.mdxq.mall.entity.Order;
import com.mdxq.mall.entity.OrderItem;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController()
@RequestMapping("/api/v1/order")
public class OrderController {

    @GetMapping("getOrder/{id}")
    public ResponseEntity<Order> getOrder(@PathVariable Long id){
        OrderItem orderItem1=new OrderItem(1l,"乔丹鞋",100f,1);
        OrderItem orderItem2=new OrderItem(2l,"乒乓球拍",99f,1);
        OrderItem orderItem3=new OrderItem(3l,"别克牌钢笔",50f,2);
        List<OrderItem>orderItemList=new ArrayList<>();
        orderItemList.add(orderItem1);
        orderItemList.add(orderItem2);
        orderItemList.add(orderItem3);
        Order order=new Order();
        order.setId(id);
        order.setOrderTime(LocalDateTime.now());
        order.setOrderItemList(orderItemList);
        float price=0;
        for (OrderItem orderItem : orderItemList) {
            price+=orderItem.getPrice()*orderItem.getCount();
        }
        order.setTotalPrice(price);
        return ResponseEntity.ok(order);
    }
}
